package com.xbai.goods.controller;

import com.github.pagehelper.PageInfo;
import com.xbai.entity.Result;
import com.xbai.goods.pojo.UndoLog;
import com.xbai.goods.service.UndoLogService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author jxbai
 * @desc UndoLogController
 * @date 2021/1/28
 */

@RestController
@RequestMapping("/undoLog")
@CrossOrigin
@RequiredArgsConstructor
public class UndoLogController {

    private final UndoLogService undoLogService;

    /**
     * UndoLog分页条件搜索实现
     *
     * @param undoLog {@link UndoLog}
     * @param page    页码
     * @param size    页大小
     * @return {@link Result<PageInfo>}
     */
    @PostMapping(value = "/search/{page}/{size}")
    public Result<PageInfo> findPage(@RequestBody(required = false) UndoLog undoLog, @PathVariable int page, @PathVariable int size) {
        //调用UndoLogService实现分页条件查询UndoLog
        PageInfo<UndoLog> pageInfo = undoLogService.findPage(undoLog, page, size);
        return new Result<>(pageInfo);
    }

    /**
     * UndoLog分页搜索实现
     *
     * @param page 页码
     * @param size 页大小
     * @return {@link Result<PageInfo>}
     */
    @GetMapping(value = "/search/{page}/{size}")
    public Result<PageInfo> findPage(@PathVariable int page, @PathVariable int size) {
        //调用UndoLogService实现分页查询UndoLog
        PageInfo<UndoLog> pageInfo = undoLogService.findPage(page, size);
        return new Result<>(pageInfo);
    }

    /***
     * 多条件搜索UndoLog数据
     * @param undoLog {@link UndoLog}
     * @return {@link Result}
     */
    @PostMapping(value = "/search")
    public Result<List<UndoLog>> findList(@RequestBody(required = false) UndoLog undoLog) {
        //调用UndoLogService实现条件查询UndoLog
        List<UndoLog> list = undoLogService.findList(undoLog);
        return new Result<>(list);
    }

    /***
     * 根据ID删除UndoLog数据
     * @param id 主键id
     * @return {@link Result}
     */
    @DeleteMapping(value = "/{id}")
    public Result delete(@PathVariable Long id) {
        //调用UndoLogService实现根据主键删除
        undoLogService.delete(id);
        return new Result();
    }

    /***
     * 修改UndoLog数据
     * @param undoLog {@link UndoLog}
     * @param id 主键id
     * @return {@link Result}
     */
    @PutMapping(value = "/{id}")
    public Result update(@RequestBody UndoLog undoLog, @PathVariable Long id) {
        //设置主键值
        undoLog.setId(id);
        //调用UndoLogService实现修改UndoLog
        undoLogService.update(undoLog);
        return new Result();
    }

    /***
     * 新增UndoLog数据
     * @param undoLog {@link UndoLog}
     * @return {@link Result}
     */
    @PostMapping
    public Result add(@RequestBody UndoLog undoLog) {
        //调用UndoLogService实现添加UndoLog
        undoLogService.add(undoLog);
        return new Result();
    }

    /***
     * 根据ID查询UndoLog数据
     * @param id 主键id
     * @return {@link Result}
     */
    @GetMapping("/{id}")
    public Result<UndoLog> findById(@PathVariable Long id) {
        //调用UndoLogService实现根据主键查询UndoLog
        UndoLog undoLog = undoLogService.findById(id);
        return new Result<>(undoLog);
    }

    /***
     * 查询UndoLog全部数据
     * @return {@link Result}
     */
    @GetMapping
    public Result<List<UndoLog>> findAll() {
        //调用UndoLogService实现查询所有UndoLog
        List<UndoLog> list = undoLogService.findAll();
        return new Result<>(list);
    }
}
